package fun.lifepoem.blog.config;

import fun.lifepoem.blog.filter.HeaderFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Yiwyn
 * @create 2022/9/12 22:26
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")            //允许访问的路径
                .allowedOriginPatterns("*")    //允许请求的域名
                .allowedMethods("GET", "POST", "DEL", "PUT", "OPTIONS") //允许请求的方法
                .allowCredentials(true)        //是否允许证书
                .allowedHeaders("*")        //允许的请求头属性
                .maxAge(3600);            //跨域允许时间
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(headerFilter()).
                addPathPatterns("/**").
                order(-10);
    }

    public HeaderFilter headerFilter() {
        return new HeaderFilter();
    }


}
